<?php
/**
 * Frontend album view
 *
 * @package Bd Gallery
 * @author Bebliuc George Cristian
 * @author Gianfranco Liporace
 * @copyright 2008-2010
 * @version 1.3
 * @access public
 * @license GPL 2
 *
 */

    $settings = Plugin::getAllSettings('gallery');
    $page = URL_PUBLIC . $settings['gallery_page'];
    $image_path = URL_PUBLIC . "public/" . $settings['images_folder'];

    $thumbs_per_row = $settings['thumbs_per_row'];
    $count = 0;
    $table = "<table id='gal_table'><tr>";
    if (isset($tags)) {
        $categories = Category::findByTags($tags);
    } else {
        $categories = Category::findAllFrom('Category');
    }
    // Display a table with category thumbnails
    if (!isset($cid)) {
        foreach ($categories as $category) {
            $photos = Photos::findAllByCategoryId($category->getId());
            if ($count == $thumbs_per_row) {
                $table .= "</tr><tr>";
                $count = 0;
            }
            // Display only non-empty albums
            if (count($photos) > 0) {
                $image_url = $image_path . 'tb_' . $photos[0]->getFilename();           
                $table .= "<td align='center'><a href='{$page}/{$category->getId()}'><img src='{$image_url}' class='thumbnail'></a>";
                $table .= "<div id='gal_description'>{$category->getDescription()}</div></td>";
            }
            $count++;
        }
     } else
        // Thumbnail Listing
        if (isset($cid)) {
            $photos = Photos::findAllByCategoryId($cid);
            if (count($photos) == 0) {
                $result_final = 'This category does not exist or there are no photos in it.';
            } else {
                $category = Category::findById($cid);
                $photos = Photos::findAllByCategoryId($cid);
                foreach ($photos as $photo) {
                    if ($count == $thumbs_per_row) {
                        $table .= "</tr><tr>";
                        $count = 0;
                    }
                    $table .= "<td>";
                    if ($settings['script'] == "shadowbox") {
                        $table .= "<a href='view_photo/{$photo->getId()}'
                                      rel=\"shadowbox[{$category->getName()}];options={handleOversize:'resize'}\"
                                      title=\"{$photo->getCaption()}\">
                                      <img src='{$image_path}/tb_{$photo->getFilename()}'
                                           border='0'
                                           alt='{$photo->getCaption()}'
                                           class=\"thumbnail\"/></a>";
                    } elseif ($settings['script'] == "none") {
                        $table .= "<a href='{$page}/{$cid}/{$photo->getId()}'>
                                      <img src='{$image_path}/tb_{$photo->getFilename()}'
                                           border='0'
                                           alt='{$photo->getCaption()}'
                                           class=\"thumbnail\" /></a>";
                    } elseif ($settings['script'] == "slimbox") {
                        $table .= "<a href='view_photo/{$photo->getId()}'
                                      rel=\"lightbox[{$category->getName()}]\"
                                      title=\"{$photo->getCaption()}\">
                                      <img src='{$image_path}/tb_{$photo->getFilename()}'
                                           border='0'
                                           alt='{$photo->getCaption()}'
                                           class=\"thumbnail\"/></a>";
                    } elseif ($settings['script'] == "popup") {
                        $table .= "<a href='javascript:;' 
                                      onClick=\"openWin('{$image_path}{$photo->getFilename()}'); return false\">
                                      <img src='{$image_path}/tb_{$photo->getFilename()}'
                                           border='0' alt='{$photo->getTitle()}'
                                           class=\"thumbnail\" /></a>";
                    }
                    $table .= "<div id='gal_description'>{$photo->getTitle()}</div></td>";
                    $table .= "</td>";
                    $count++;

                }
            }
        }
    $table .= "</tr></table>";
    echo $table;
?>
